package com.pybox.api;

import com.pybox.dto.Result;
import com.pybox.dto.TDeployment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LeiYang
 * @title: TWorkFlowApi
 * @projectName pybox
 * @description: 流程部署信息
 * @date 2019/8/615:22
 */
@RequestMapping("/TActivit")
@Api(tags = "TActivit")
@Slf4j
@RestController
public class TWorkFlowApi {

    @Autowired
    private RepositoryService repositoryService;


    /**
     * 查询部署对象信息，对应表（act_re_deployment）
     */
    @ApiOperation(value = "流程部署信息", notes = "流程部署信息")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "pageNo", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", paramType = "query")
    })
    public Result<Page<TDeployment>> getActivitDeployment(
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize) {

        Pageable pageable = new PageRequest(pageNo - 1, pageSize);
        List<Deployment> list = repositoryService.createDeploymentQuery()//创建部署对象查询
                .orderByDeploymenTime().asc()//
                .list();
        List<TDeployment> lists = new ArrayList<>();

        // 循环(定义TDeployment是为了解决activit 读取Task时和springboot结合时的懒加载异常)
        for (Deployment deployment : list) {
            TDeployment ideployment = new TDeployment();
            ideployment.setDeploymentId(deployment.getId());
            ideployment.setDeploymentName(deployment.getName());
            ideployment.setDeploymentTime(deployment.getDeploymentTime());
            lists.add(ideployment);
        }
        Page<TDeployment> page = new PageImpl<>(lists, pageable, list.size());
        return Result.ok(page);
    }

    /**
     * 根据Id查询申请单
     */

    @ApiOperation(value = "根据流程部署(deploymentId)，查询流程定义详情", notes = "根据流程部署(deploymentId)，查询流程定义详情")
    @RequestMapping(value = "/fetchActivitDe/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "DeploymentId id", dataType = "String", paramType = "path")
    public Result<TDeployment> fetchActivitDe(@PathVariable("id") String deploymentId) {
        /**查询流程定义的信息，对应表（act_re_procdef）*/

        ProcessDefinition processDefinition = repositoryService.
                createProcessDefinitionQuery().deploymentId(deploymentId).singleResult(); //创建流程定义查询,根据部署ID
        TDeployment tDeployment = new TDeployment();

        /*流程定义ID*/
        tDeployment.setProcessDefinitionId(processDefinition.getId());
        /*名称*/
        tDeployment.setProcessDefinitionName(processDefinition.getName());
        /* 流程定义KEY*/
        tDeployment.setProcessDefinitionKey(processDefinition.getKey());
        /*流程定义版本*/
        tDeployment.setProcessDefinitionVersion(processDefinition.getVersion());
        /*  流程定义规则文件名称*/
        tDeployment.setProcessDefinitionGuiZeName(processDefinition.getResourceName());
        /*流程定义规则图片名称*/
        tDeployment.setProcessDefinitionPhotoName(processDefinition.getDiagramResourceName());
        /*  部署ID*/
        tDeployment.setDeploymentId(deploymentId);

        return Result.ok(tDeployment);
    }

    /**
     * 使用部署对象ID和资源图片名称，获取图片的输入流
     */
    @ApiOperation(value = "使用部署对象ID和资源图片名称，获取图片的输入流", notes = "使用部署对象ID和资源图片名称，获取图片的输入流")
    @RequestMapping(value = "/findImageInputStream/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "DeploymentId id", dataType = "String", paramType = "path")
    @ResponseBody
    public void findImageInputStream(@PathVariable("id") String deploymentId, HttpServletResponse response) {

        /**查询流程定义的信息，对应表（act_re_procdef）*/

        ProcessDefinition processDefinition = repositoryService.
                createProcessDefinitionQuery().deploymentId(deploymentId).singleResult(); //创建流程定义查询,根据部署ID
        String imageName = processDefinition.getDiagramResourceName();
        repositoryService.getResourceAsStream(deploymentId, imageName);
       // response.getHeaders();
    }
}
